package com.example.librarymanagement.util;

import com.example.librarymanagement.util.convert.DtoConvert;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;


/**
 * E 为实体类对象
 *
 * @author wx
 */
public class BaseDto<E> implements Serializable {
    /**
     * 将Dto对象转换为实体类对象
     *
     * @param entity 实体类对象
     */
    public void convertToEntity(E entity) {
        BaseDtoConvert<BaseDto<E>> baseDtoConvert = new BaseDtoConvert<>();
        baseDtoConvert.convert(entity, this);
    }

    /**
     * @param <D> Dto对象
     */
    private class BaseDtoConvert<D extends BaseDto<E>> implements DtoConvert<D, E> {
        @Override
        public void convert(E entity, D baseDto) {
            BeanUtils.copyProperties(baseDto, entity);
        }
    }
}


